package com.goldsprite.microhorigame.screens.games;

import com.badlogic.gdx.*;
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.graphics.*;
import com.goldsprite.gameframeworks.*;

public class CameraTool
 {
	public static InputAdapter createCamZoomEvent(OrthographicCamera worldCamera, Stage uiStage, float[] outZoom) {
		return new InputAdapter(){
				float initialDistance = 0f;
				float currentDistance = 0f;
				boolean isZooming = false;
				public float moveRate = 1.2f;
				final Vector3 last = new Vector3(-1, -1, -1);

			private boolean[] isUIEvent = new boolean[]{ false };

				//桌面端
				@Override
				public boolean scrolled(float amountX, float amountY) {
					float zoom = worldCamera.zoom;
					float sensitivity = zoom;
					float val = amountY * 0.3f * sensitivity;

					outZoom[0] = zoom + val;
					return super.scrolled(amountX, amountY);
				}

				//安卓端
				com.badlogic.gdx.math.Vector2 stagePos = new com.badlogic.gdx.math.Vector2();
				Vector3 tmpPos = new Vector3();
				float lastZoomFactor;
				float targetZoom;
				Actor[] hitActor2 = new Actor[2];
				@Override
				public boolean touchDragged(int x, int y, int pointer) {
					uiStage.screenToStageCoordinates(stagePos.set(x, y));
					Actor hit = uiStage.hit(stagePos.x, stagePos.y, true);
					if (pointer == 0) hitActor2[0] = hit;
					if (pointer == 1) hitActor2[1] = hit;
					//LogViewerService.logDebug("hit0-"+(hitActor2[0]==null?"null":hitActor2[0].getClass().getSimpleName()) +", hit1: "+(hitActor2[1]==null?"null":hitActor2[1].getClass().getSimpleName()));

					//if(hitActor2[0] != guiTable || hitActor2[1] != guiTable) return false;
					if (Gdx.input.isTouched(0) && Gdx.input.isTouched(1)) {
						int x1 = Gdx.input.getX(0);
						int y1 = Gdx.input.getY(0);
						int x2 = Gdx.input.getX(1);
						int y2 = Gdx.input.getY(1);

						if (!isZooming) {
							// 初始化双指缩放状态
							initialDistance = calculateDistance(x1, y1, x2, y2);
							isZooming = true;
							targetZoom = worldCamera.zoom;
						}
						else {

							// 计算缩放
							currentDistance = calculateDistance(x1, y1, x2, y2);
							float zoomFactor = currentDistance - initialDistance;
							zoomFactor /= Gdx.graphics.getWidth();
							zoomFactor *= 3;
							float zoomFactorDiff = zoomFactor - lastZoomFactor;
							if(Math.abs(zoomFactor) > .01f){
								targetZoom = targetZoom - zoomFactor;
								outZoom[0] = targetZoom;
								lastZoomFactor = zoomFactor;
								initialDistance = currentDistance;
							}
							//LogViewerService.log("zooming: "+nextCamZoom+", zoomFactor: "+zoomFactor);
						}
					}

					return false;
				}

				@Override
				public boolean touchUp(int x, int y, int pointer, int button) {
					// 停止缩放或拖动
					if (!Gdx.input.isTouched(0) || !Gdx.input.isTouched(1)) {
						isZooming = false;
					}
					last.set(-1, -1, -1);
					return false;
				}

				private float calculateDistance(int x1, int y1, int x2, int y2) {
					return (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
				}

			};
	}

	 public static InputProcessor createCamController(OrthographicCamera cam) {
		 return new InputAdapter(){
			 float startX, startY;
			 float startCamX, startCamY;
			 @Override
			 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
				startX = screenX;
				startY = screenY;
				startCamX = cam.position.x;
				startCamY = cam.position.y;
				return super.touchDown(screenX, screenY, pointer, button);
			 }

			 @Override
			 public boolean touchDragged(int screenX, int screenY, int pointer) {
				 float moveX = screenX - startX;
				 float moveY = screenY - startY;
				 float newX = startCamX - moveX * cam.zoom;
				 float newY = startCamY - (-moveY) * cam.zoom;
				 cam.position.set(newX, newY, 0);
				 return super.touchDragged(screenX, screenY, pointer);
			 }
		 };
	 }
 }
